﻿#include "Obiect3D.h"

Obiect3D::Obiect3D( Tip t , DrawMode d)
{
	SetTip(t);
	SetDesen(d);
	translat[0] = translat[1] = translat[2] = 0;
	rotatie[0] = rotatie[1] = rotatie[2] = 0;
	scalare[0] = scalare[1] = scalare[2] = 1.0;
	SetLevelOfDetail(5);
	culoare[3] = 1;
	culoare_sel[3] = 1;
	selected = false;
	selectable = true;
};

void Obiect3D::select(void)
{
	selected = true;
};

void Obiect3D::deselect(void)
{
	selected = false;
};

void Obiect3D::SetPozitie(float x, float y, float z)
{
	pozitie[0] = x;
	pozitie[1] = y;
	pozitie[2] = z;
};

void Obiect3D::SetTranslat(float x, float y, float z)
{
	translat[0] += x;
	translat[1] += y;
	translat[2] += z;
};
void Obiect3D::SetScalare(float x, float y, float z)
{
	scalare[0] *= x;
	scalare[1] *= y;
	scalare[2] *= z;
};

void Obiect3D::SetRotatie(float x, float y, float z)
{
	rotatie[0] += x;
	rotatie[1] += y;
	rotatie[2] += z;
};

void Obiect3D::SetCuloare(float r, float g, float b)
{
	culoare[0] = r;
	culoare[1] = g;
	culoare[2] = b;
};

void Obiect3D::SetCuloare_sel(float r, float g, float b)
{
	culoare_sel[0] = r;
	culoare_sel[1] = g;
	culoare_sel[2] = b;
};

void Obiect3D::SetTip(Tip t)
{
	tip = t;
};

void Obiect3D::SetDesen(DrawMode d)
{
	desen = d;
};

float* Obiect3D::GetPozitie()
{
	return &pozitie[0];
};

float* Obiect3D::GetTranslat()
{
	return &translat[0];
};

float* Obiect3D::GetScalare()
{
	return &scalare[0];
};

float* Obiect3D::GetRotatie()
{
	return &rotatie[0];
};

float* Obiect3D::GetCuloare()
{
	return &culoare[0];
};

float* Obiect3D::GetCuloare_sel()
{
	return &culoare_sel[0];
};

Tip Obiect3D::GetTip()
{
	return tip;
};

DrawMode Obiect3D::GetDesen()
{
	return desen;
};

void Obiect3D::SetLevelOfDetail(float l)
{
	level = l;
};

void Obiect3D::SetAlpha(float alpha)
{
	culoare[3] = culoare_sel[3] = alpha;
};

void Obiect3D::SetCoords(float *cx, float *cy,float *cz)
{
	for (int i = 0 ; i < 8 ; i++){
			coords[i][0] = cx[i];
			coords[i][1] = cy[i];
			coords[i][2] = cz[i];
	};
};

void Obiect3D::draw()
{

	glPushMatrix();
	
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
    glEnable(GL_DEPTH_TEST);
	glEnable(GL_LIGHTING);

	// translatie
	glTranslatef(translat[0], translat[1],translat[2]);

	// rotatie
	glRotatef( rotatie[0] , 1.0 , 0.0 , 0.0 );
	glRotatef( rotatie[1] , 0.0 , 1.0 , 0.0 );
	glRotatef( rotatie[2] , 0.0 , 0.0 , 1.0 );

	// scalare
	glScalef( scalare[0] , scalare[1] , scalare[2]);

	
	float a[4] ={0.1,0.1,0.1,1.0};
	if( selected == false )
	{
		glColor4f(culoare[0],culoare[1],culoare[2],culoare[3]);
		glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,a);
		glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,a);
	}
	else
	{
		culoare_sel[0] = culoare[0] *5;
		culoare_sel[1] = culoare[1] *5;
		culoare_sel[2] = culoare[2] *5;
		culoare_sel[3] = culoare[3];
		glColor4f(culoare_sel[0],culoare_sel[1],culoare_sel[2],culoare_sel[3]);

		glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,culoare_sel);
	}
	
	
	if( desen == Wire )
		switch( tip )
	{
		case Cube :	glutWireCube(1.0); break;
		case Sphere : glutWireSphere(1.0, level, level); break;
		case Cone : glutWireCone(1.0, 1.5, level,level); break;
		case Teapot : glutWireTeapot(1.0); break;
		case Pyramid:
			culoare[3]=0.4;
			selectable = false;
			glColor4f(culoare[0],culoare[1],culoare[2],culoare[3]);
			glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
			glBegin(GL_TRIANGLE_FAN);     	// draw triangle               	// set color to red
			glVertex3f(  coords[0][0],  coords[0][1], coords[0][2]);
			glVertex3f(coords[1][0], coords[1][1], coords[1][2]);
			glVertex3f( coords[2][0], coords[2][1], coords[2][2]);
			glVertex3f( coords[3][0], coords[3][1], coords[3][2]);
			glVertex3f( coords[4][0], coords[4][1], coords[4][2]);
			glVertex3f(coords[1][0], coords[1][1], coords[1][2]);
			glEnd();
			glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
			glBegin(GL_QUADS);
			glVertex3f(coords[1][0], coords[1][1], coords[1][2]);// draw square
			glVertex3f( coords[4][0], coords[4][1], coords[4][2]);
			glVertex3f( coords[3][0], coords[3][1], coords[3][2]);
			glVertex3f( coords[2][0], coords[2][1], coords[2][2]);
			glEnd();
			break;
		case Block:
			selectable = false;
			culoare[3]=0.4;
			glColor4f(culoare[0],culoare[1],culoare[2],culoare[3]);
			glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
			glBegin(GL_QUADS);     
			glVertex3f(coords[0][0], coords[0][1], coords[0][2]);
			glVertex3f( coords[4][0], coords[4][1], coords[4][2]);
			glVertex3f( coords[5][0], coords[5][1], coords[5][2]);
			glVertex3f( coords[1][0], coords[1][1], coords[1][2]);
			glEnd();
			glBegin(GL_QUADS);     
			glVertex3f(coords[1][0], coords[1][1], coords[1][2]);
			glVertex3f( coords[5][0], coords[5][1], coords[5][2]);
			glVertex3f( coords[6][0], coords[6][1], coords[6][2]);
			glVertex3f( coords[2][0], coords[2][1], coords[2][2]);
			glEnd();
			glBegin(GL_QUADS);     
			glVertex3f(coords[2][0], coords[2][1], coords[2][2]);
			glVertex3f( coords[6][0], coords[6][1], coords[6][2]);
			glVertex3f( coords[7][0], coords[7][1], coords[7][2]);
			glVertex3f( coords[3][0], coords[3][1], coords[3][2]);
			glEnd();
			glBegin(GL_QUADS);     
			glVertex3f(coords[0][0], coords[0][1], coords[0][2]);
			glVertex3f( coords[1][0], coords[1][1], coords[1][2]);
			glVertex3f( coords[2][0], coords[2][1], coords[2][2]);
			glVertex3f( coords[3][0], coords[3][1], coords[3][2]);
			glEnd();
			glBegin(GL_QUADS);
			glVertex3f(coords[4][0], coords[4][1], coords[4][2]);// draw square
			glVertex3f( coords[5][0], coords[5][1], coords[5][2]);
			glVertex3f( coords[6][0], coords[6][1], coords[6][2]);
			glVertex3f( coords[7][0], coords[7][1], coords[7][2]);
			glEnd();
			glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
			break;
		case Linie:
			glBegin(GL_LINES);     
			glVertex3f(coords[0][0], coords[0][1], coords[0][2]);
			glVertex3f( coords[1][0], coords[1][1], coords[1][2]);
			glEnd();
			break;
	}
	else
		switch( tip )
	{
		case Cube :	glutSolidCube(1.0); break;
		case Sphere : glutSolidSphere(1.0, level, level); break;
		case Cone : glutSolidCone(1.0, 2.0, level,level); break;
		case Teapot : glutSolidTeapot(1.0); break;
	}

	glPopMatrix();

};
Obiect3D::~Obiect3D(void)
{
};
